package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICustomerTermRuleService;
import com.mdd.admin.validate.CustomerTermRuleCreateValidate;
import com.mdd.admin.validate.CustomerTermRuleUpdateValidate;
import com.mdd.admin.validate.CustomerTermRuleSearchValidate;
import com.mdd.admin.vo.CustomerTermRuleListedVo;
import com.mdd.admin.vo.CustomerTermRuleDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.CustomerTermRule;
import com.mdd.common.mapper.CustomerTermRuleMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户期限规则实现类
 * @author LikeAdmin
 */
@Service
public class CustomerTermRuleServiceImpl implements ICustomerTermRuleService {
        
    @Resource
    CustomerTermRuleMapper customerTermRuleMapper;

    /**
     * 客户期限规则列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CustomerTermRuleListedVo>
     */
    @Override
    public PageResult<CustomerTermRuleListedVo> list(PageValidate pageValidate, CustomerTermRuleSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<CustomerTermRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        customerTermRuleMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:caseTypeId@case_type_id:str",
            "=:caseType@case_type:str",
            "=:handlingInformation@handling_information:str",
            "=:caseStageId@case_stage_id:str",
            "=:caseStage@case_stage:str",
            "=:nation:str",
            "=:applicationTypeId@application_type_id:str",
            "=:applicationType@application_type:str",
            "=:businessTypeId@business_type_id:str",
            "=:businessType@business_type:str",
            "=:interiorBaseDate@interior_base_date:str",
            "=:interiorAddSubtractOrder@interior_add_subtract_order:str",
            "=:interiorMonthlyIncrement@interior_monthly_increment:str",
            "=:interiorDaysIncrement@interior_days_increment:str",
            "=:customerBaseDate@customer_base_date:str",
            "=:customerAddSubtractOrder@customer_add_subtract_order:str",
            "=:customerMonthlyIncrement@customer_monthly_increment:str",
            "=:customerDaysIncrement@customer_days_increment:str",
            "=:officialBaseDate@official_base_date:str",
            "=:officialAddSubtractOrder@official_add_subtract_order:str",
            "=:officialMonthlyIncrement@official_monthly_increment:str",
            "=:officialDaysIncrement@official_days_increment:str",
        });

        IPage<CustomerTermRule> iPage = customerTermRuleMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CustomerTermRuleListedVo> list = new LinkedList<>();
        for(CustomerTermRule item : iPage.getRecords()) {
            CustomerTermRuleListedVo vo = new CustomerTermRuleListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户期限规则详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return CustomerTermRule
     */
    @Override
    public CustomerTermRuleDetailVo detail(Integer id) {
        CustomerTermRule model = customerTermRuleMapper.selectOne(
                new QueryWrapper<CustomerTermRule>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CustomerTermRuleDetailVo vo = new CustomerTermRuleDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户期限规则新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(CustomerTermRuleCreateValidate createValidate) {
        CustomerTermRule model = new CustomerTermRule();
        model.setCaseTypeId(createValidate.getCaseTypeId());
        model.setCaseType(createValidate.getCaseType());
        model.setHandlingInformation(createValidate.getHandlingInformation());
        model.setCaseStageId(createValidate.getCaseStageId());
        model.setCaseStage(createValidate.getCaseStage());
        model.setNation(createValidate.getNation());
        model.setApplicationTypeId(createValidate.getApplicationTypeId());
        model.setApplicationType(createValidate.getApplicationType());
        model.setBusinessTypeId(createValidate.getBusinessTypeId());
        model.setBusinessType(createValidate.getBusinessType());
        model.setInteriorBaseDate(createValidate.getInteriorBaseDate());
        model.setInteriorAddSubtractOrder(createValidate.getInteriorAddSubtractOrder());
        model.setInteriorMonthlyIncrement(createValidate.getInteriorMonthlyIncrement());
        model.setInteriorDaysIncrement(createValidate.getInteriorDaysIncrement());
        model.setCustomerBaseDate(createValidate.getCustomerBaseDate());
        model.setCustomerAddSubtractOrder(createValidate.getCustomerAddSubtractOrder());
        model.setCustomerMonthlyIncrement(createValidate.getCustomerMonthlyIncrement());
        model.setCustomerDaysIncrement(createValidate.getCustomerDaysIncrement());
        model.setOfficialBaseDate(createValidate.getOfficialBaseDate());
        model.setOfficialAddSubtractOrder(createValidate.getOfficialAddSubtractOrder());
        model.setOfficialMonthlyIncrement(createValidate.getOfficialMonthlyIncrement());
        model.setOfficialDaysIncrement(createValidate.getOfficialDaysIncrement());
        customerTermRuleMapper.insert(model);
    }

    /**
     * 客户期限规则编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(CustomerTermRuleUpdateValidate updateValidate) {
        CustomerTermRule model = customerTermRuleMapper.selectOne(
                new QueryWrapper<CustomerTermRule>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCaseTypeId(updateValidate.getCaseTypeId());
        model.setCaseType(updateValidate.getCaseType());
        model.setHandlingInformation(updateValidate.getHandlingInformation());
        model.setCaseStageId(updateValidate.getCaseStageId());
        model.setCaseStage(updateValidate.getCaseStage());
        model.setNation(updateValidate.getNation());
        model.setApplicationTypeId(updateValidate.getApplicationTypeId());
        model.setApplicationType(updateValidate.getApplicationType());
        model.setBusinessTypeId(updateValidate.getBusinessTypeId());
        model.setBusinessType(updateValidate.getBusinessType());
        model.setInteriorBaseDate(updateValidate.getInteriorBaseDate());
        model.setInteriorAddSubtractOrder(updateValidate.getInteriorAddSubtractOrder());
        model.setInteriorMonthlyIncrement(updateValidate.getInteriorMonthlyIncrement());
        model.setInteriorDaysIncrement(updateValidate.getInteriorDaysIncrement());
        model.setCustomerBaseDate(updateValidate.getCustomerBaseDate());
        model.setCustomerAddSubtractOrder(updateValidate.getCustomerAddSubtractOrder());
        model.setCustomerMonthlyIncrement(updateValidate.getCustomerMonthlyIncrement());
        model.setCustomerDaysIncrement(updateValidate.getCustomerDaysIncrement());
        model.setOfficialBaseDate(updateValidate.getOfficialBaseDate());
        model.setOfficialAddSubtractOrder(updateValidate.getOfficialAddSubtractOrder());
        model.setOfficialMonthlyIncrement(updateValidate.getOfficialMonthlyIncrement());
        model.setOfficialDaysIncrement(updateValidate.getOfficialDaysIncrement());
        customerTermRuleMapper.updateById(model);
    }

    /**
     * 客户期限规则删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        CustomerTermRule model = customerTermRuleMapper.selectOne(
                new QueryWrapper<CustomerTermRule>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        customerTermRuleMapper.updateById(model);
    }

}
